package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.Constitution;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.cms.service.ConstitutionService;

import javax.servlet.http.HttpServletRequest;

import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.cms.dto.ConstitutionQueryDTO;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author NOW_GO
 * @desc ConstitutionController
 * @date 2019-07-11
 */
@Controller
@RequestMapping("/channel/constitution")
public class ConstitutionController {

    @Autowired
    private ConstitutionService constitutionService;
    @Autowired
    private ColumnInfoService columnInfoService;
    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String title = request.getParameter("title");
        String columnId = request.getParameter("columnId");
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        String  xhzcId ="";
        List<ColumnInfo> ggfgCoulumnInfo = columnInfoService.findByCode(Constants.CODE_JSPD_JSGX_XHZC); // 协会章程
        if (!CollectionUtils.isEmpty(ggfgCoulumnInfo)) {
            ColumnInfo ggfg = ggfgCoulumnInfo.get(0);
            xhzcId = ggfg.getId();
        }
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setRootColumnId(xhzcId);
        columnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> xhzcIdCoulumnInfoList = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        ConstitutionQueryDTO constitutionQueryDTO = new ConstitutionQueryDTO();
        constitutionQueryDTO.setCurrentPage(currentPage);
        constitutionQueryDTO.setPageSize(pageSize);
        constitutionQueryDTO.setTitle(title);
        constitutionQueryDTO.setColumnId(columnId);
        PageModel<Constitution> page = this.constitutionService.queryConstitutionPage(constitutionQueryDTO);
        model.addAttribute("page", page);
        model.addAttribute("constitutionQueryDTO", constitutionQueryDTO);
        model.addAttribute("xhzcIdCoulumnInfoList", xhzcIdCoulumnInfoList);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_CONSTITUTION);

        return "/channel/constitution/constitution_list";
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        String  xhzcId ="";
        List<ColumnInfo> ggfgCoulumnInfo = columnInfoService.findByCode(Constants.CODE_JSPD_JSGX_XHZC); // 法律法规
        if (!CollectionUtils.isEmpty(ggfgCoulumnInfo)) {
            ColumnInfo ggfg = ggfgCoulumnInfo.get(0);
            xhzcId = ggfg.getId();
        }
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setRootColumnId(xhzcId);
        columnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> xhzcIdCoulumnInfoList = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);


        Constitution constitution = null;
        if (StringUtils.isNotBlank(id)) {
            constitution = this.constitutionService.find(id);
        }
        model.addAttribute("constitution", constitution);
        model.addAttribute("id", id);
        model.addAttribute("xhzcIdCoulumnInfoList", xhzcIdCoulumnInfoList);
        return "/channel/constitution/constitution_edit";
    }

    @RequestMapping("/saveorupdate")
    @ResponseBody
    public AjaxResult doSaveOrUpdate(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        try {
            String id = request.getParameter("id");
            String columnId = request.getParameter("columnId");
            String title = request.getParameter("title");
            String description = request.getParameter("description");
            Constitution constitution = null;
            if (StringUtils.isNotBlank(id)) {
                constitution = this.constitutionService.find(id);
                constitution.setColumnId(columnId);
                constitution.setDescription(description);
                constitution.setTitle(title);
                constitution.setUpdateDate(new Date());
                this.constitutionService.update(constitution);
            } else {
                constitution = new Constitution();
                constitution.setColumnId(columnId);
                constitution.setDescription(description);
                constitution.setTitle(title);
                constitution.setUpdateDate(new Date());
                constitution.setCreateDate(new Date());
                this.constitutionService.save(constitution);
            }
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
        }

       return ajaxResult;
    }

    @RequestMapping("/doDelete")
    @ResponseBody
    public AjaxResult doDelete(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        String id = request.getParameter("id");

        try {
            Constitution constitution = this.constitutionService.find(id);
            if (constitution != null) {
                constitution.setDeleteFlag("1");
                this.constitutionService.update(constitution);
            }
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxResult;
    }


}